/*
 * jQuery Maxlength
 * http://pioul.fr/jquery-maxlength
 *
 * Copyright 2013, Philippe Masset
 * Dual licensed under the MIT or GPL Version 2 licenses
 */
(function($){
	$.fn.maxlength = function(options){
		var t = $(this);
		t.each(function(){
			options = $.extend(
				{},
				{
					counterContainer: false,
					text: '%left characters left' // %length %maxlength %left
				},
				options
			);
			var t = $(this),
				data = {
					options: options,
					field: t,
					/*counter: $('<div class="maxlength"></div>'),*/
					counter: $('.maxlength'),
					maxLength: parseInt(t.attr("maxlength"), 10),
					lastLength: null,
					updateCounter: function(){
						var length = this.field.val().length,
							text = this.options.text.replace(/\B%(length|maxlength|left)\b/g, $.proxy(function(match, p){
								return (p == 'length')? length : (p == 'maxlength')? this.maxLength : (this.maxLength - length);
							}, this));
						
						if(text < 0){
							text = 0;
						}
						if(length == data.maxLength){
							text = '<span style="color:red;">已达到最大字符数！</span>';
						}
						this.counter.html(text);
						if(length != this.lastLength){
							this.updateLength(length);
						}
					},
					updateLength: function(length){
						this.field.trigger("update.maxlength", [
							this.field,
							this.lastLength,
							length,
							this.maxLength,
							this.maxLength - length
						]);
						this.lastLength = length;
					}
				};
			if(data.maxLength){
				data.field
					.data("maxlength", data)
					.bind({
						"keyup change input propertychange": function(e){
							$(this).data("maxlength").updateCounter();
						},
						"cut paste drop": function(e){
							setTimeout($.proxy(function(){
								$(this).data("maxlength").updateCounter();
							}, this), 1);
						}
					});
				if(options.counterContainer){
					options.counterContainer.append(data.counter);
				} else {
					data.field.after(data.counter);
				}
				data.updateCounter();
			}
		});
		return t;
	};
})(jQuery);